package com.metadata_system.common.service;

import com.metadata_system.common.db.entity.User;
import com.metadata_system.common.db.entity.UserLoginLog;
import com.metadata_system.common.db.entity.UserOperationLog;
import com.metadata_system.common.db.mapper.UserMapper;
import com.metadata_system.common.db.mapper.UserOperationLogMapper;
import com.metadata_system.common.dto.ApiResult;
import com.metadata_system.common.request.UserLoginLogListParams;
import com.metadata_system.common.request.UserOperationLogListParams;
import com.metadata_system.common.utils.AddressUtil;
import com.metadata_system.common.utils.DateTimeUtil;
import com.metadata_system.system.response.UserLoginLogResponse;
import com.metadata_system.system.response.UserOperationLogResponse;
import lombok.SneakyThrows;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserOperationLogService {

    @Autowired(required = false)
    private UserOperationLogMapper userOperationLogMapper;
    @Autowired(required = false)
    UserMapper userMapper;

    /**
     * 操作日志-新增
     * @param userOperationLog
     */
    public void insert(UserOperationLog userOperationLog) {
        User user = this.userMapper.selectByPrimaryKey(userOperationLog.getUserId());
        userOperationLog.setCreatedTime(new Date());
        userOperationLog.setUsername(user.getUsername());
        userOperationLog.setRole(user.getRole());
        userOperationLog.setIp(AddressUtil.GetAddressIp());
        userOperationLog.setFile(StringUtils.isBlank(userOperationLog.getFile())?"":userOperationLog.getFile());
        // 2.是否推送到ai-server: 0.否 1.是
        userOperationLog.setIsPushAiServer(0);
        userOperationLogMapper.insert(userOperationLog);
    }


    /**
     * 登录日志-列表
     */
    @SneakyThrows
    public ApiResult getList(UserOperationLogListParams listParams) {
        Example example = new Example(UserOperationLog.class);
        Example.Criteria criteria = example.createCriteria();

        // 1.查询条件参数
        // 1-2.普通查询参数
        if (listParams.getUserId()!=null){
            criteria.andEqualTo("userId", listParams.getUserId());
        }
        if (!StringUtils.isBlank(listParams.getRole())) {
            criteria.andEqualTo("role", listParams.getRole());
        }
        if (!StringUtils.isBlank(listParams.getUsername())) {
            criteria.andLike("username", "%"+listParams.getUsername()+"%");
        }
        if (!StringUtils.isBlank(listParams.getContent())) {
            criteria.andLike("content", "%"+listParams.getContent()+"%");
        }
        if (!StringUtils.isBlank(listParams.getIp())) {
            criteria.andLike("ip", "%"+listParams.getIp()+"%");
        }

        listParams.setConditionTime(listParams.getCreatedTime(),criteria,"createdTime");
        listParams.setOrder(example,"createdTime,id");

        // 3.查询: 总条数,列表数据
        int total = this.userOperationLogMapper.selectCountByExample(example);
        List<UserOperationLog> userLoginLogs = new ArrayList<>();
        if (total > 0) {
            userLoginLogs = this.userOperationLogMapper.selectByExampleAndRowBounds(example, listParams.getRowRunds());
        }
        return ApiResult.successPage(UserOperationLogResponse.formatData(userLoginLogs), total);
    }


    public Integer getDeleteIdByUpdateTime(String updatedTime) {
        Example example = new Example(UserOperationLog.class);
        Example.Criteria criteria = example.createCriteria();

        String updatedTime1="";
        String[] split = updatedTime.split(",");


        criteria.andEqualTo("updatedTime", updatedTime);
        return this.userOperationLogMapper.selectOneByExample(example).getId();
    }
}
